#include "utility.h"

char* Config::filename = NULL;

bool Config::Init(LPCTSTR _filename) {
	char dir[505];
	int size = 500, len;
	GetModuleFileName(NULL, dir, size);
	len = strlen(dir);
	while(dir[len-1] != '\\') len--;
	dir[len] = 0;
	strcat(dir, _filename);

	if(filename) {
		delete[] filename;
		filename = NULL;
	}
	filename = new char[strlen(dir) + 1];
	strcpy(filename, dir);
	return true;
}

DWORD Config::ReadProperty(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPTSTR lpReturnedString) {
	if(filename == NULL) Init("config.ini");
	int nSize = 100;
	DWORD hr = GetPrivateProfileString(lpAppName, lpKeyName, NULL, lpReturnedString, nSize, filename);
	return hr;
}

BOOL Config::WriteProperty(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPTSTR lpString) {
	if(filename == NULL) Init("config.ini");
	BOOL hr = WritePrivateProfileString(lpAppName, lpKeyName, lpString, filename);
	return hr;
}
